package com.dzqc.fly.common;


import com.dzqc.fly.db.form.SecretaryForm;

import java.lang.reflect.Field;
import java.util.*;


/**
 * @Auther: HLP
 * @Date: 2022/8/2 16:42
 * 工具类：获取子类和父类所以属性，将对象转为map
 */
public class Kv {
    public static Map<String, Object> toKv(Object object){
        Map<String,Object> dataMap = new HashMap<>();
        Field[] fields=getAllFields(object);
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                dataMap.put(field.getName(),field.get(object));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return dataMap;
    }
    public static Field[] getAllFields(Object object){
        Class clazz = object.getClass();
        List fieldList = new ArrayList<>();
        while (clazz != null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
            clazz = clazz.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }
//    public static void main(String[] args) {
//        Map<String, String> srcMap = new HashMap<>();
//        srcMap.put("a","b");
//        srcMap.put("c","c");
//        System.out.println(srcMap.put("r","d"));
//        System.out.println(srcMap);
//    }
}